<template>
  <!-- 列表 -->
  <ContentWrap>
    <!-- <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['school:notice:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button> -->
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column
        label="首次查看时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column
        label="最近查看时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column label="查看次数" align="center" prop="readCount" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <NoticeRecordForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { NoticeApi } from '@/api/school/notice'
import NoticeRecordForm from './NoticeRecordForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  noticeId?: number // 通知ID（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  noticeId: undefined as unknown
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await NoticeApi.getNoticeRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.noticeId) {
    message.error('请选择一个通知')
    return
  }
  formRef.value.open(type, id, props.noticeId)
}
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.noticeId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.noticeId = val
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>
